package es.wtestgen.util;

import java.io.Serializable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorResources;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.Resources;

public class WTestGenValidador implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * Returns true if obj is null or a String.
     */
    private static boolean isString(Object obj) {
        return (obj == null) ? true : String.class.isInstance(obj);
    }
    
    /**
     * Método para validar que la longitud de un campo de entrada de datos es
     * de una cantidad de caracteres exacta
     * @param bean
     * @param v
     * @param va
     * @param field
     * @param errors
     * @param request
     * @return
     */
    
    public static boolean validateExactLength(Object bean,
    										Validator v,
    		                                ValidatorAction va, Field field,
    		                                ActionMessages errors,
    		                                HttpServletRequest request) {
    		 
         String value = null;
         if (isString(bean)) {
             value = (String) bean;
         } else {
        	 value = ValidatorUtils.getValueAsString(bean, field.getProperty());
         }
		 
         if (value != null) {
        	 try {
	                 int length = Integer.parseInt(field.getVarValue("exactlength"));
    		 
	                 //if (!GenericValidator.maxLength(value, length) || !GenericValidator.minLength(value, length )) {
	                 if(value.length() != length){
	                     //errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
	                	 errors.add(field.getKey(), Resources.getActionMessage(v,request, va, field));
		 
	                     return false;
	                 }
	             } catch (Exception e) {
    		                 //errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
    		                 errors.add(field.getKey(), Resources.getActionMessage(v,request, va, field));
    		                 return false;
	             }
	         }
		 
         return true;
     }
    
    
    /**
     * Método para validar que dos campos de entrada de datos son 
     * iguales
     * @param bean
     * @param v
     * @param va
     * @param field
     * @param errors
     * @param request
     * @return
     */
    public static boolean validateConfirmPassword(Object bean,
											Validator v,
								            ValidatorAction va, Field field,
								            ActionMessages errors,
								            HttpServletRequest request) {
		
    	String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		String sProperty2 = field.getVarValue("pwdUsua");
		String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);
		
		if (!GenericValidator.isBlankOrNull(value)) {
			try {
				if (!value.equals(value2)) {
					//errors.add(field.getKey(),Resources.getActionError(request, va, field));
					errors.add(field.getKey(),Resources.getActionMessage(v,request, va, field));
		
					return false;
				}
			} catch (Exception e) {
				//errors.add(field.getKey(),Resources.getActionError(request, va, field));
				errors.add(field.getKey(),Resources.getActionMessage(v,request, va, field));
		
				return false;
			}
		}
		
		return true;
    }
    
    
    public static boolean validateTwoFields(
    	    Object bean,
    	    ValidatorAction va, 
    	    Field field,
    	    //ActionErrors errors,
    	    ActionMessages errors,
    	    HttpServletRequest request, 
    	    ServletContext application) {

    	    String value = ValidatorUtils.getValueAsString(
    	        bean, 
    	        field.getProperty());
    	    String sProperty2 = field.getVarValue("secondProperty");
    	    String value2 = ValidatorUtils.getValueAsString(
    	        bean, 
    	        sProperty2);

    	    if (!GenericValidator.isBlankOrNull(value)) {
    	       try {
    	          if (!value.equals(value2)) {
    	             errors.add(field.getKey(),
    	            	Resources.getActionMessage(request, va, field)
    	            	/*
    	                Resources.getActionError(
    	                    application,
    	                    request,
    	                    va,
    	                    field)*/);

    	             return false;
    	          }
    	       } catch (Exception e) {
    	             errors.add(field.getKey(),
    	            		 Resources.getActionMessage(request, va, field)
    	                /*Resources.getActionError(
    	                    application,
    	                    request,
    	                    va,
    	                    field)*/
    	            		 );
    	             return false;
    	       }
    	    }

    	    return true;
    	}
    
    

}
